/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.faytechcc.csc.gui.search;

import com.sun.rowset.CachedRowSetImpl;
import edu.faytechcc.csc.messages.NetworkConnection;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.ResultSetMetaData;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 * Please describe what this class does here.
 *
 * CSC 251 0001 DATE:
 *
 * @author Stephanie Mckenzie
 */
public class ListPanel extends JPanel
{

    final int PANEL_WIDTH = 300;
    final int PANEL_HEIGHT = 200;
    private String[][] tableData;
    private String[] colNames;
    private String tableName = "Anime_Table";
    private JTable table;
    private DefaultTableModel model;
    private NetworkConnection conn;
    private SearchFrame search;

    public ListPanel(SearchFrame searching)
    {
        this.search = searching;
        this.conn = search.getConn();
        conn.sendMessages("SELECT ENG_TITLE, GENERA FROM Anime_Table "
                + "ORDER BY ENG_TITLE");
        table = new JTable(model);
        table.addMouseListener(new TableMouseListener());
        table.enableInputMethods(false);
        JScrollPane scrollPane = new JScrollPane(table);
        // set size of scrollPane
        scrollPane.setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));

        // add things
        this.add(scrollPane);
    }

    public void setTable(CachedRowSetImpl resultSet)
    {
        try
        {
            resultSet.last();
            int numRows = resultSet.getRow();
            resultSet.first();

            ResultSetMetaData meta = resultSet.getMetaData();
            int columCount = meta.getColumnCount();
            colNames = new String[columCount];
            //get the colum names
            for (int index = 0; index < columCount; index++)
            {
                colNames[index] = meta.getColumnLabel(index + 1);
            }
            //create a 2D string array for the table data
            tableData = new String[numRows][columCount];
            //Store the colums in the tableDate array
            for (int row = 0; row < numRows; row++)
            {
                for (int col = 0; col < columCount; col++)
                {
                    tableData[row][col] = resultSet.getString(col + 1);
                }
                resultSet.next();
            }
            model = new DefaultTableModel(tableData, colNames);
            table.setModel(model);

        } catch (Exception ex)
        {
            System.out.println("Table not created");
        }
    }

    /**
     * @return the tableName
     */
    public String getTableName()
    {
        return tableName;
    }

    /**
     * @param tableName the tableName to set
     */
    public void setTableName(String tableName)
    {
        this.tableName = tableName;
    }

    private class TableMouseListener implements MouseListener
    {

        @Override
        public void mouseClicked(MouseEvent e)
        {
            int row = table.getSelectedRow();
            String title = (model.getValueAt(row, 0)).toString();
            conn.sendMessages("SELECT * FROM " + tableName
                + " WHERE ENG_TITLE = '" + title + "'");            
        }

        @Override
        public void mousePressed(MouseEvent e)
        {
        }

        @Override
        public void mouseReleased(MouseEvent e)
        {
        }

        @Override
        public void mouseEntered(MouseEvent e)
        {
        }

        @Override
        public void mouseExited(MouseEvent e)
        {
        }
    }
}
